package com.thinkgem.jeesite.modules.website;

import com.thinkgem.jeesite.modules.area_info.entity.AreaInfo;
import com.thinkgem.jeesite.modules.area_info.service.AreaInfoService;
import com.thinkgem.jeesite.modules.area_info_his.entity.AreaInfoHis;
import com.thinkgem.jeesite.modules.area_info_his.service.AreaInfoHisService;
import com.thinkgem.jeesite.modules.lane.entity.Lane;
import com.thinkgem.jeesite.modules.lane.service.LaneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xlp
 * @date 2019/5/14
 */
@RestController
@RequestMapping("/lane")
public class LaneInfoController {

    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private LaneService laneService;

    @Autowired
    private AreaInfoHisService hisService;

    /**
     * 获取区域
     * @param request
     * @return
     */
    @RequestMapping(value = "/getAreaList", method = RequestMethod.POST)
    public List<AreaInfo> getCompanyInfo(HttpServletRequest request) {
        List<AreaInfo> areaInfoList = areaInfoService.findList(new AreaInfo());
        if (CollectionUtils.isEmpty(areaInfoList)){
            return null;
        }
        return areaInfoList;
    }

    /**
     * 新增或修改车道信息
     * @param request
     * @param lane
     */
    @RequestMapping(value = "/addOrUpdateLane", method = RequestMethod.POST)
    public String addOrUpdateLane(HttpServletRequest request, Lane lane) {
        laneService.save(lane);
        return "";
    }

    /**
     * 获取单个车道信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/getLane", method = RequestMethod.POST)
    public Lane getLaneList(HttpServletRequest request, String id) {
        Lane lane = laneService.get(id);
        return lane;
    }

    /**
     * 获取车道信息集合
     * @param request
     * @return
     */
    @RequestMapping(value = "/getLaneList", method = RequestMethod.POST)
    public ResponseData getLaneList(HttpServletRequest request) {
        List<Lane> laneList = laneService.findList(new Lane());
        ResponseData responseData = new ResponseData();
        if (!CollectionUtils.isEmpty(laneList)){
            for (Lane lane:laneList){
                AreaInfo areaInfo = areaInfoService.get(lane.getAreaId());
                if (areaInfo!=null){
                    lane.setAreaName(areaInfo.getAreaName());
                }
            }
            responseData.setCode(200);
            responseData.setMsg("查询成功");
            responseData.setTotal(String.valueOf(laneList.size()));
            responseData.setData(laneList);
        }else {
            responseData.setCode(200);
            responseData.setMsg("查询失败");
            responseData.setTotal("0");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 删除/批量删除车道信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(HttpServletRequest request, String id) {
        String res = "";
        String[] ids = id.split(",");
        Lane lane = new Lane();
        try {
            for (int i=0; i<ids.length; i++){
                lane.setId(ids[i]);
                laneService.delete(lane);
            }
            res = "200";
        }catch (Exception e){
            e.printStackTrace();
            res = "201";
        }
        return res;
    }

    /**
     * 获取A、B区域各车道的剩余车位数（约定好前三个是A区域，后六个是B区域）
     * @param request
     * @return
     */
    @RequestMapping(value = "/getLaneRemainSpace", method = RequestMethod.POST)
    public Map<String, Object> getLaneRemainSpace(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        //各车道剩余车位
        List<String> stringList = new ArrayList<String>();
        //A、B车道总车位
        long totalA = 0;
        long totalB = 0;

        List<AreaInfo> areaInfoList = areaInfoService.findList(new AreaInfo());
        for (AreaInfo areaInfo:areaInfoList){
            //A区域只查三个车道
            if ("A区".equals(areaInfo.getAreaName())){
                Lane lane = new Lane();
                lane.setAreaId(areaInfo.getId());
                List<Lane> laneList = laneService.findList(lane);
                if (!CollectionUtils.isEmpty(laneList)) {
                    if (laneList.size()>3){
                        for (int i=0; i<3; i++){
                            String remainSpace = laneList.get(i).getRemainSpace();
                            stringList.add(remainSpace);

                            totalA += Long.valueOf(laneList.get(i).getTotalSpace());
                        }
                    }else {
                        for (Lane lane1:laneList){
                            String remainSpace = lane1.getRemainSpace();
                            stringList.add(remainSpace);

                            totalA += Long.valueOf(lane1.getTotalSpace());
                        }
                        int i = 3 - stringList.size();
                        for (int j=0; j<i; j++){
                            stringList.add("");
                        }
                    }
                }else{
                    stringList.add("");
                    stringList.add("");
                    stringList.add("");
                }
            }
            if ("B区".equals(areaInfo.getAreaName())){
                Lane lane = new Lane();
                lane.setAreaId(areaInfo.getId());
                List<Lane> laneList = laneService.findList(lane);
                if (!CollectionUtils.isEmpty(laneList)) {
                    if (laneList.size()>6){
                        for (int i=0; i<6; i++){
                            String remainSpace = laneList.get(i).getRemainSpace();
                            stringList.add(remainSpace);

                            totalB += Long.valueOf(laneList.get(i).getTotalSpace());
                        }
                    }else {
                        for (Lane lane1:laneList){
                            String remainSpace = lane1.getRemainSpace();
                            stringList.add(remainSpace);

                            totalB += Long.valueOf(lane1.getTotalSpace());
                        }
                        int i = 9 - stringList.size();
                        for (int j=0; j<i; j++){
                            stringList.add("");
                        }
                    }
                }else{
                    stringList.add("");
                    stringList.add("");
                    stringList.add("");
                    stringList.add("");
                    stringList.add("");
                    stringList.add("");
                }
            }
        }
        map.put("data", stringList);
        map.put("totalA", totalA);
        map.put("totalB", totalB);
        return map;
    }

    /**
     * 分时段统计
     * @param request
     * @param param
     * @return
     */
    @RequestMapping(value = "/displayByHour", method = RequestMethod.POST)
    public ResponseData displayByHour(HttpServletRequest request, String param){
        ResponseData responseData = new ResponseData();
        AreaInfoHis areaInfoHis = new AreaInfoHis();
        areaInfoHis.setDelFlag(param);
        List<AreaInfoHis> areaInfoHisList = hisService.findList(areaInfoHis);
        if(!CollectionUtils.isEmpty(areaInfoHisList)){
            responseData.setData(areaInfoHisList);
            responseData.setMsg("查询成功");
            responseData.setCode(200);
        }else {
            responseData.setData(null);
            responseData.setMsg("查询失败");
            responseData.setCode(201);
        }
        return responseData;
    }
}
